package com.um.jdy.ykc.command;

import com.um.jdy.ykc.utils.RadixsUtil;
import io.netty.buffer.ByteBuf;
import io.netty.buffer.Unpooled;
import lombok.Data;

/**
 * 远程更新（运营平台->充电桩）
 *
 * @author 甘天伟
 * @since 2022/6/28
 * 版权所有 广州优匠科技有限公司
 */
@Data
public class Cmd94UpgradeDeviceRequest extends YkcCommand {

    /**
     * 桩编码：7个字节
     */
    private String code;

    /**
     * 桩型号：1个字节
     */
    private int  mode;

    /**
     * 桩功率：2个字节
     */
    private int  power;

    /**
     * 升级服务器地址：16个字节
     */
    private String  host;

    /**
     * 升级服务器端口：2个字节
     */
    private int  port;

    /**
     * 用户名：16个字节
     */
    private String  username;

    /**
     * 密码：16个字节
     */
    private String  password;

    /**
     * 文件路径：32个字节
     */
    private String  url;

    /**
     * 执行控制：1个字节
     */
    private int  exeType;

    /**
     * 下载超时时间：1个字节 单位:min
     */
    private int  timeout;


    public Cmd94UpgradeDeviceRequest(int serial,String code,int mode,int power,String host,int port,String username,String password,
                                    String url,int exeType,int timeout) {
        super();

        this.serial = serial;
        this.encFlag = RadixsUtil.hexToInteger("00");
        this.command = RadixsUtil.hexToInteger("94");

        this.code = code;
        this.mode = mode;
        this.power = power;
        this.host = host;
        this.port = port;
        this.username = username;
        this.password = password;
        this.url = url;
        this.exeType = exeType;
        this.timeout = timeout;

        byte[] codeByte = RadixsUtil.hexToBytes(code);
        byte[] hostByte = RadixsUtil.asciiToBytes(host);
        byte[] usernameByte = RadixsUtil.asciiToBytes(username);
        byte[] passwordByte = RadixsUtil.asciiToBytes(password);
        byte[] urlByte = RadixsUtil.asciiToBytes(url);

        ByteBuf buff = Unpooled.buffer();
        buff.writeBytes(fillZeroBytes(codeByte,7 - codeByte.length));
        buff.writeByte(mode);
        buff.writeBytes(RadixsUtil.int16ToBytes(power));
        buff.writeBytes(fillZeroBytes(hostByte,16 - hostByte.length));
        buff.writeBytes(RadixsUtil.int16ToBytes(port));
        buff.writeBytes(fillZeroBytes(usernameByte,16 - usernameByte.length));
        buff.writeBytes(fillZeroBytes(passwordByte,16 - passwordByte.length));
        buff.writeBytes(fillZeroBytes(urlByte,32 - urlByte.length));
        buff.writeByte(exeType);
        buff.writeByte(timeout);


        this.data = new byte[buff.readableBytes()];
        buff.readBytes(data);
        buff.release();

        postConstruct();
    }
}
